import matplotlib.pyplot as plt
import pandas as pd


def get_pfs_Data(filename):
    filePath = './dataSets/' + str(filename) + '.csv'
    data = pd.read_csv(filePath)
    length = len(data['s'])
    x = range(1, length + 1)
    y = data['pfs']
    # 年份分割线
    x1 = []
    for i in range(1, 6):
        x1.append(i * 12.5)
    # 点
    dotList = []
    maxList = []  # 最高点
    for i in range(1, length + 1):
        if i % 12 == 1:
            s = int(i / 12)
            m = max(y[s * 12: (s + 1) * 12])  # 获取每年最高点
        style = 'ob'  # 设置最高点，蓝色
        if (y[i - 1] == m):  # 每年最高点
            maxList.append([i, y[i - 1]])  # 收集最高点位置及风速
            style = 'om'  # 设置最高点，紫红颜色
        dotList.append([i, y[i - 1], style])
    figureSet = {}
    figureSet['plot'] = [[x, y]]
    figureSet['axvline'] = [[x1, 'orange']]  # x1为列表
    figureSet['dot'] = dotList  # 包括最高点
    figureSet['annotate'] = maxList  # 备注月份
    return figureSet


def figure(figureSet, **attrs):
    # 默认值
    attr = {
        'figureNum': 0,
        'tfont': 20,
        'xfont': 20,
        'yfont': 20,
        'figsize': (30, 10),
        'text': (0, 0.5),
        'title': 'title',
        'xlabel': 'xlabel',
        'ylabel': 'ylabel',
        'xticks': range(1, 72 + 1),
        'save': False,
        'filename': 'title.png',
    }
    # 赋初值
    for i in attrs:
        attr[i] = attrs[i]
    fig = plt.figure(num=attr['figureNum'], figsize=attr['figsize'])
    sub_pic = fig.add_subplot(1, 1, 1)
    sub_pic.set_title(attr['title'], fontsize=attr['tfont'])
    # 设置x轴名称,plt.xlabel
    sub_pic.set_xlabel(attr['xlabel'], fontsize=attr['xfont'])
    # 设置y轴名称,plt.ylabel
    sub_pic.set_ylabel(attr['ylabel'], fontsize=attr['yfont'])
    sub_pic.xaxis.grid(True, which='major')
    sub_pic.set_xticks(attr['xticks'])
    # 画曲线 figureSet['plot']
    for i in figureSet['plot']:
        x, y = i
        sub_pic.plot(x, y)
    # 分割线 figureSet['axvline']
    for i in figureSet['axvline']:
        x, color = i
        for j in x:
            sub_pic.axvline(j, color=color)
    # 标点 figureSet['dot']
    for i in figureSet['dot']:
        x, y, style = i
        sub_pic.plot(x, y, style)
    # 备注月份
    for i in figureSet['annotate']:
        x, y = i
        sub_pic.annotate(str(x % 12), xy=(x, y),
                         xytext=(x + attr['text'][0], y + attr['text'][1]))
    # 保存图片
    if attr['save']:
        fig.savefig(attr['filename'])
